define(function(require) {
	var $ = require("jquery");
	// var justep = require("$UI/system/lib/justep");
	// var fn = require("$UI/ls_common/fn");
	var Model = function() {
		this.callParent();
		this.SaleApp_ret = {};
	};

	Model.prototype.StartBtnClick = function(event) {
		console.log("StartBtnClick() event=", event);
		var SDID = event.bindingContext.$object.val('SDID'); // 获取点击按钮的xid
		console.log(this.SaleApp_ret);
		this.getParent().comp("TaskDetailsWindowContainer").load("Factory/Warehouse/TaskDetails_Sale.w", {
			'SDID' : SDID,
			'SaleApp_ret' : this.SaleApp_ret
		});

		this.getParent().comp("FactoryContents").to("TaskDetailsContent");
	};

	Model.prototype.modelLoad = function(event) {
		console.log("modelLoad()");
	};

	Model.prototype.getTaskList = function() {
		console.log("getTaskList()");
		var data = {};
		data.action = window.app.UserCJApi_ret.Conn;
		var self = this;
		var api_url = window.app.base_url + window.app.SaleApp;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				$.extend(self.SaleApp_ret, res);

				console.log("this.SaleApp_ret=", self.SaleApp_ret);
				self.comp("ID_List").clear();
				self.comp("SDID_List").clear();
				self.comp("ID_List").loadData(res);

				var data = self.comp('ID_List');
				data.each(function(param) {
					var row = self.comp("SDID_List").find([ 'SDID' ], [ param.row.val('SDID') ], true);
					if (row.length <= 0) {
						self.comp("SDID_List").add({
							'SDID' : param.row.val('SDID'),
							'SDCode' : param.row.val('SDCode'),
							'CtmName' : param.row.val('CtmName'),
							'CtmNo' : param.row.val('CtmNo'),
							'DateDelivery' : param.row.val('DateDelivery'),
							'ID_array' : " " + param.row.val('ID')
						});
					} else {
						console.log("重复了.............");
						var str_a = row[0].val('ID_array');
						var arr_a = str_a.split(',');
						arr_a.push(param.row.val('ID'));
						var str_b = arr_a.toString();
						row[0].val('ID_array', str_b);
						console.log(row[0].val('ID_array'));
					}
				});
				console.log(self.comp("SDID_List"));

				return;
			},
			error : function(err) {
				console.log(err);
				return;
			}
		});
	};
	Model.prototype.modelParamsReceive = function(event) {
		console.log(event);
		this.getTaskList();

	};

	Model.prototype.backBtnClick = function(event) {
		this.getParent().comp("FactoryContents").to("SelectContent");
	};

	return Model;
});